package cate.game.role.res.guild.msg.apply;

import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdGuild;
import cate.game.role.Role;
import cate.game.role.res.guild.po.GuildPO;

import java.util.ArrayList;
import java.util.List;

@ClientMsg(title = "快速加入公会的请求")
public class GuildFastJoinReq implements MsgInput {
    @Override
    public MsgConfig conf() {
        return MsgIdGuild.GUILD_FAST_JOIN_REQ;
    }

    @Override
    public void execute(GameBody game, ClientContext cc) {
        Role role = cc.getRole();
        if (!role.getRes().guild.checkGuild().ok()) {
            // 判断惩罚时间
            if (role.getRes().guild.withdrawalTime > 0) {
                long pastSecond = (System.currentTimeMillis() - role.getRes().guild.withdrawalTime) / 1000;
                long punishSecond = game.res.guild.action.getPunishSecond(role.getGame());
                if (pastSecond < punishSecond) {
                    long dif = punishSecond - pastSecond;
                    int hour = (int) dif / 3600;
                    int min = (int) (dif - hour * 3600) / 60;
                    List<Integer> params = new ArrayList<>();
                    params.add(hour);
                    params.add(min);
                    role.getGame().notice.message(role, String.format("%d小时%d分后才能申请加入新的公会", params.get(0), params.get(1)));
                    return;
                }
            }
            GuildPO guild = game.res.guild.findAvailableGuild(cc.getRole());
            if (guild == null) {
                game.notice.message(role,"没有可加入的公会");
                return;
            }
            guild.apply.handleJoinExe(role, role, true);
        } else {
            game.notice.message(role,"您已加入过公会");
        }
    }
}
